<!doctype html>
<title>Canvas loss and restoration when size is extremely large and then restored to a reasonable value</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
async_test(t => {
    var canvas = document.createElement('canvas')
    canvas.addEventListener('contextlost', t.step_func(contextLost));
    canvas.addEventListener('contextrestored', t.step_func(contextRestored));
    var ctx = canvas.getContext('2d');
    var lostEventHasFired = false;
    verifyContextLost(false);

    var bigsize = 1000000000;
    canvas.width = bigsize;
    canvas.height = bigsize;
    verifyContextLost(true);
    canvas.width = bigsize;
    verifyContextLost(true);
    // Restore a reasonable dimension
    canvas.width = 100;
    canvas.height = 100;
    verifyContextLost(true); // Restoration is async.

    function verifyContextLost(shouldBeLost) {
        // Verify context loss experimentally as well as isContextLost()
        ctx.fillStyle = '#0f0';
        ctx.fillRect(0, 0, 1, 1);
        var contextLostTest = ctx.getImageData(0, 0, 1, 1).data[1] == 0;
        assert_equals(contextLostTest, shouldBeLost, 'image data is blank');
        assert_equals(ctx.isContextLost(), shouldBeLost, 'context is lost');
    }

    function contextLost() {
        assert_false(lostEventHasFired, 'contextlost event has fired');
        lostEventHasFired = true;
        verifyContextLost(true);
    }

    function contextRestored() {
        assert_true(lostEventHasFired, 'contextlost event has fired');
        verifyContextLost(false);
        t.done();
    }
});
</script>
